﻿package lobby
{
	import fl.controls.UIScrollBar;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.text.TextField;
	import flash.net.URLRequest;
	import lobby.events.LobbyEvent;
	import lobby.exchangeddata.HashTable;
	import lobby.interfaces.ILanguage;
	import flash.ui.Keyboard;
	
	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)
	 */
	
	public class FormAgreement extends MovieClip implements ILanguage
	{
		public var scrollBarRule:UIScrollBar;
		public var txtTitleUserTf:TextField;
		public var txtAgreementTf:TextField;
		public var txtAcceptTf:TextField;
		public var txtNotAcceptTf:TextField;
		public var btnAcceptUser:SimpleButton;
		public var btnNotAcceptUser:SimpleButton;
		
		private var urlRequest:URLLoader;
		
		public function FormAgreement()
		{
			this.visible = false;
			this.txtAcceptTf.mouseEnabled = false;
			this.txtNotAcceptTf.mouseEnabled = false;
			this.btnAcceptUser.addEventListener(MouseEvent.CLICK, hdlAccept);
			this.btnNotAcceptUser.addEventListener(MouseEvent.CLICK, hdlAccept);
			this.urlRequest = new URLLoader();
			this.urlRequest.dataFormat = URLLoaderDataFormat.VARIABLES;
			this.urlRequest.addEventListener(Event.COMPLETE, hdlCompleteTxt);
			this.addEventListener(KeyboardEvent.KEY_DOWN, hdlEnter);
			this.btnAcceptUser.tabEnabled = false;
			this.btnNotAcceptUser.tabEnabled = false;
		}
		
		private function hdlEnter(e:KeyboardEvent):void 
		{
			if (e.keyCode == Keyboard.ENTER)
			{
				trace("enter in aggrement form");
				dispatchEvent(new LobbyEvent(LobbyEvent.ACCEPT_AGRE, true));
			}
		}
		
		private function hdlAccept(e:Event):void
		{
			if (e.currentTarget == this.btnAcceptUser)
				dispatchEvent(new LobbyEvent(LobbyEvent.ACCEPT_AGRE, true));
			else if (e.currentTarget == this.btnNotAcceptUser)
				dispatchEvent(new LobbyEvent(LobbyEvent.NOT_ACCEPT_AGRE, true));
			txtAgreementTf.text = "";
		}
		
		private function hdlCompleteTxt(e:Event):void
		{
			this.txtAgreementTf.htmlText = e.target.data["data"];
			this.scrollBarRule.setSize(815, 511);
			this.scrollBarRule.update();
			this.scrollBarRule.setScrollPosition(0);
			stage.focus = this.txtTitleUserTf;
		}
		
		public function loadTxt(nameTxt:String):void
		{
			this.urlRequest.load(new URLRequest(nameTxt));
		}
		
		public function changeTextLang(info:HashTable):void
		{
			txtTitleUserTf.text = info.lookupString("userguide");
			txtAcceptTf.text = info.lookupString("Btnaccept");
			txtNotAcceptTf.text = info.lookupString("Btnnotaccept");
		}
	}

}